package com.ruoyi.lyj.member.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lyj.consumption.domain.Consumption;
import com.ruoyi.lyj.consumption.service.IConsumptionService;
import com.ruoyi.lyj.recharge.domain.Recharge;
import com.ruoyi.lyj.recharge.service.IRechargeService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lyj.member.mapper.MemberMapper;
import com.ruoyi.lyj.member.domain.Member;
import com.ruoyi.lyj.member.service.IMemberService;

/**
 * 会员Service业务层处理
 * 
 * @author lcl
 * @date 2021-03-26
 */
@Service
public class MemberServiceImpl implements IMemberService 
{
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private IRechargeService rechargeService;

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IMemberService memberService;

    /**
     * 查询会员
     * 
     * @param id 会员ID
     * @return 会员
     */
    @Override
    public Member selectMemberById(Long id)
    {
        return memberMapper.selectMemberById(id);
    }

    /**
     * 查询会员列表
     * 
     * @param member 会员
     * @return 会员
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增会员
     * 
     * @param member 会员
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        member.setCreateTime(DateUtils.getNowDate());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改会员
     * 
     * @param member 会员
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除会员
     * 
     * @param ids 需要删除的会员ID
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(Long[] ids)
    {
        return memberMapper.deleteMemberByIds(ids);
    }

    /**
     * 删除会员信息
     * 
     * @param id 会员ID
     * @return 结果
     */
    @Override
    public int deleteMemberById(Long id)
    {
        return memberMapper.deleteMemberById(id);
    }


    @Override
    public void rechargeAccount(Recharge recharge) {
        //充值
        Member member = memberService.selectMemberById(recharge.getMemberId());
        Long account = member.getAccount();
        Long rechargeAccount = recharge.getAccount();
        member.setUpdateBy(SecurityUtils.getUsername());
        member.setUpdateTime(new Date());
        member.setAccount(account + rechargeAccount);
        memberService.updateMember(member);
        //添加充值记录
        recharge.setCreateTime(new Date());
        recharge.setCreateBy(SecurityUtils.getUsername());
        rechargeService.insertRecharge(recharge);
    }

    @Override
    public void consumptionAccount(Consumption consumption) {
        //消费
        Member member = memberService.selectMemberById(consumption.getMemberId());
        Long account = member.getAccount();
        Long consumptionAccount = consumption.getAccount();
        member.setUpdateBy(SecurityUtils.getUsername());
        member.setUpdateTime(new Date());
        member.setAccount(account - consumptionAccount);
        memberService.updateMember(member);
        //添加消费记录
        consumption.setCreateTime(new Date());
        consumption.setCreateBy(SecurityUtils.getUsername());
        consumptionService.insertConsumption(consumption);
    }
}
